package com.southwind.controller;

import com.netflix.discovery.converters.Auto;
import com.southwind.entity.Order;
import com.southwind.entity.OrderVO;
import com.southwind.repository.OrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;


@RestController
@RequestMapping("/order")
public class OrderHandler {
    //    读取启动时的端口
    @Value("${server.port}")
    private String port;
    @Autowired
    private OrderRepository orderRepository;

    @GetMapping("/index")
    public String index() {
        return "当前order的端口是" + this.port;
    }

    @PostMapping("/save")
    public void save(@RequestBody Order order) {
        order.setDate(LocalDate.now());
        orderRepository.save(order);
    }

    @GetMapping("/findByUid/{index}/{limit}/{uid}")
    public OrderVO findByUid(@PathVariable("index") Integer index, @PathVariable("limit") Integer limit, @PathVariable("uid") Integer uid) {

        return new OrderVO(0, "", orderRepository.countByUid(uid), orderRepository.findByUid(index, limit, uid));
    }

    @GetMapping("/countByUid/{uid}")
    public Integer countByUid(@PathVariable("uid") Integer uid) {
        return orderRepository.countByUid(uid);
    }

    @GetMapping("/findAllOrder/{index}/{limit}")
    public OrderVO findAllOrder(@PathVariable("index") Integer index, @PathVariable("limit") Integer limit) {
        OrderVO orderVO = new OrderVO();
        orderVO.setCode(0);
        orderVO.setMsg("");
        orderVO.setCount(orderRepository.count());
        orderVO.setData(orderRepository.findAllByState(index, limit));
        return orderVO;
    }
    @PutMapping("/updateState/{id}")
    public void updateState(@PathVariable("id") Integer id){
        orderRepository.updateState(id);
    }

}
